<?php
namespace Api\Controller;
use Admin\Logic\CacheLogic;

/**
 * 数据进入接口，全部要验证key
 */
class IndexController extends JsonController {

    /**
     * 自动执行函数
     */
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: * ");
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
        header('Access-Control-Allow-Headers:X-Requested-With,x-requested-with,content-type,accept,Content-Type, Accept,requesttype');
    }
    
    /**
     * 手机号撞库
     */
    public function check_loan_mobile()
    {
        $jsonStr = file_get_contents("php://input");
        if (!empty($jsonStr)) {
            $data = json_decode($jsonStr, true);
            $checkMsg = $this->vaildKey($data['key']);
            if ($checkMsg !== true) {
                $this->errorjsonReturn($checkMsg);
            }
            $mobile = $data['mobile'];
            if (empty($mobile)) {
                $this->errorjsonReturn('密文不能为空');
            }
            $info = M('first_customer')->query("select id from yq_first_customer where MD5(mobile) = '{$mobile}' limit 1");
            if ($info) {
                $this->errorjsonReturn('手机号已存在');
            } else {
                $this->setjsonReturn('校验通过');
            }
        } else {
            $this->errorjsonReturn('请求参数不能为空');
        }
    }
    
    
    /**
     * 推入数据接口
     */
    public function add()
    {
        //处理请求数据
        $post = I('post.');
        $checkMsg = $this->vaildKey($post['key']);
        if ($checkMsg !== true) {
            $this->errorjsonReturn($checkMsg);
        }
        if (!$post['name']) {
            $this->errorjsonReturn('客户姓名不能为空');
        }
        if (!$post['mobile']) {
            $this->errorjsonReturn('手机号不能为空');
        }
        if (!is_phone($post['mobile'])) {
            $this->errorjsonReturn('手机号码格式错误');
        }
        if (!$post['city']) {
            $this->errorjsonReturn('城市不能为空');
        }
        if (!$post['money']) {
            $this->errorjsonReturn('贷款金额不能为空');
        }
        //根据key值判断数据来源渠道，如果key值不在所有渠道列表中，就取默认渠道
        $key_channel_manger = (new CacheLogic())->get_channel_key_manger();
        if(!in_array($post['channel'], $key_channel_manger[$post['key']]['child'])){
            $post['channel'] = $key_channel_manger[$post['key']]['default'];
        }
        if (!$post['channel']) {
            $this->errorjsonReturn('渠道标识有误');
        }
        //客户附加属性值获取
        $allocateConfig=(new CacheLogic())->get_all_config();
        $customer_ext_filed = $allocateConfig['customer_ext_filed'];
        $ext = array();
        foreach($post as $key=>$value){
            if(isset($customer_ext_filed[$key])){
                $ext[$key] = $value;
            }
        }
        //开始组装数据
        $data = array(
            'name' => $post['name'],
            'mobile' => $post['mobile'],
            'city' => $post['city'],
            'money' => $post['money'],
            'sex' => $post['sex']?$post['sex']:"",
            'has_house' => $post['has_house']?$post['has_house']:"无",
            'has_car' => $post['has_car']?$post['has_car']:"无",
            'has_baodan' => $post['has_baodan']?$post['has_baodan']:"无",
            'has_gongjijin' => $post['has_gongjijin']?$post['has_gongjijin']:"无",
            'has_shebao' => $post['has_shebao']?$post['has_shebao']:"无",
            'has_yingyezhizhao' => $post['has_yingyezhizhao']?$post['has_yingyezhizhao']:"无",
            'channel' => $post['channel'],
            'remark' => $post['remark'],
            'is_repeat' => 0,
            'create_time' => time()
        );
        //下面判断有没有重复手机号的订单
        $isExists = M('first_customer')->where(array('mobile'=>$post['mobile']))->order('id desc')->find();
        if ($isExists) {
            $this->errorjsonReturn('手机号已存在');
        }
        //将数据先写入数据库
        $res = M('first_customer')->add($data);
        if ($res) {
            $this->setjsonReturn('数据创建成功');
        } else {
            $this->errorjsonReturn('数据创建失败');
        }
    }
    
    /**
     * 验证密匙
     */
    private function vaildKey($key)
    {
        if (empty($key)) {
            return '密匙不能为空';
        }
        $key_channel_manger = (new CacheLogic())->get_channel_key_manger();
        if (!array_key_exists($key, $key_channel_manger)) {
            return '密匙不存在';
        }
        return true;
    }
}